package com.soccer.soccerclub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soccer.soccerclub.entity.User;
import com.soccer.soccerclub.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody User user){
        user.setCreatTime(LocalDateTime.now());
        if (user.getRole()!=2){
            //注册学生或军人，账户暂时禁用等待管理员审批
            //todo：是否需要添加通知管理员功能
            user.setStatus(1);
        }
        boolean save = userService.save(user);
        return R.ok(save);
    }

    /**
     * 用户列表
     * @param page
     * @param user
     * @return
     */
    @GetMapping("/list")
    public R list(Page<User> page,User user){
        Page<User> page1 = userService.page(page, new QueryWrapper<>(user));
        return R.ok(page1);
    }

    /**
     * 用户更新
     * @param user
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody User user){
        boolean update = userService.updateById(user);
        return R.ok(update);
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R delete(Integer id){
        boolean remove = userService.removeById(id);
        return R.ok(remove);
    }


}

